package com.daikun.common.model;

import cn.hutool.core.util.StrUtil;
import com.daikun.common.enums.BaseCountryEnum;
import com.daikun.common.enums.BaseResultEnum;
import com.daikun.common.exception.BizException;
import lombok.*;

/**
 * 表映射 java 文件对应工具类所需的参数包装类
 * @author earl created 2025-07-08
 * @version init 0.0.1
 */
public class TableConvertModel {

    /**
     * 数据库表转 mybatis 的 xml 文件
     */
    @Getter
    @Setter
    public static class TableToXmlModel extends BaseModel {
        /** 数据库链接 url */
        private String url;
        /** 需要生成的表名称 */
        private String tableName;
        /** 需要生成的表名称所属的数据库名称 */
        private String database;
        /** 用户名 */
        private String username;
        /** 密码 */
        private String password;
        /** 生成文件绝对路径 D:/a/b/c/xxx.xml，此参数为空时，将结果打印在控制台，否则将结果输出到指定的文件中 */
        private String xmlFilePath;
        /** 生成表名称对应的实体类对象 */
        private Class<?> clazz;

        public TableToXmlModel(TableToBeanModel model, String xmlFilePath, Class<?> clazz) {
            model.checkParam();
            this.url = model.getUrl();
            this.tableName = model.getTableName();
            this.database = model.getDatabase();
            this.username = model.getUsername();
            this.password = model.getPassword();
            this.xmlFilePath = xmlFilePath;
            this.clazz = clazz;
        }

        public void checkParam() throws BizException {
            if (clazz == null) {
                throw new BizException(BaseResultEnum.PARAM_ERROR, "实体类的 clazz 不能为空");
            }
            if (StrUtil.isBlank(xmlFilePath) || !xmlFilePath.trim().endsWith(".xml")) {
                throw new BizException(BaseResultEnum.PARAM_ERROR, "xml 文件路径错误");
            }
        }
    }

    /**
     * 数据库表转换实体类所需的参数对象
     */
    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    public static class TableToBeanModel extends BaseModel {
        /** 数据库链接 url */
        private String url;
        /** 需要生成的表名称 */
        private String tableName;
        /** 需要生成的表名称所属的数据库名称 */
        private String database;
        /** 用户名 */
        private String username;
        /** 密码 */
        private String password;
        /** 生成的实体类名称，非必传字段，默认表名驼峰后追加 Entity 为类名称 */
        private String entityName;

        public void checkParam() throws BizException {
            if (StrUtil.isBlank(this.url) || StrUtil.isBlank(this.tableName) || StrUtil.isBlank(this.username) || StrUtil.isBlank(this.password)) {
                throw new BizException(BaseResultEnum.PARAM_ERROR, "url tableName username password, 四个参数不能为空");
            }
            String str = "jdbc:mysql://";
            if (!this.url.startsWith(str)) {
                throw new BizException(BaseResultEnum.PARAM_ERROR, "url 格式错误");
            }
            String ax = this.url.substring(str.length());
            if (!ax.contains("/")) {
                throw new BizException(BaseResultEnum.PARAM_ERROR, "无法获取到 database 信息");
            }
            if (StrUtil.isBlank(this.database)) {
                ax = ax.split("/")[1];
                if (ax.contains("?")) {
                    this.database = ax.split("\\?")[0];
                } else {
                    this.database = ax;
                }
            }
        }
    }

    @Getter
    @RequiredArgsConstructor
    public enum TableConvertCountryEnum {
        VIETNAM(BaseCountryEnum.VIETNAM, "jdbc:mysql://1.92.111.116:3306", "test_biz_cashline"),
        MEXICO(BaseCountryEnum.MEXICO, "jdbc:mysql://1.92.69.37:3306", "test_biz_firstedu"),
        CHILE(BaseCountryEnum.CHILE, "jdbc:mysql://120.46.155.233:3306", "chile_biz_adeyocredito"),
        INDONESIA(BaseCountryEnum.INDONESIA, "jdbc:mysql://1.92.111.116:3306", "test_biz_indone"),
        COLOMBIA(BaseCountryEnum.COLOMBIA, "jdbc:mysql://120.46.178.1:3306", "test_biz_furnish"),
        THAILAND(BaseCountryEnum.THAILAND, "jdbc:mysql://1.92.111.116:3306", "test_biz_thailand"),
        ;

        private final BaseCountryEnum item;
        private final String defDbHost;
        private final String defDbName;
    }
}
